layui.use(['customUtil'], function () {
    var customUtil = layui.customUtil;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    form.render();
    var bookType = {

        _constant: {
            api: {
                queryBookTypes:"/bookType/bookType/list",
                addBookType:"/bookType/bookType/addBookType",
                editBookType:"/bookType/bookType/editBookType",
                deleteBookType:"/bookType/bookType/deleteBookType"
            }
        },

        _init: function () {
            let _this = this;
            _this._bind();
            _this._renderTable();
            _this._tableTool();
        },

        _bind: function () {
            let _this = this;
            $("#add_bookType_btn").on("click", function () {
                _this._addBookType();
            });
            $("#add_bookType_confirm_btn").on("click", function () {
                _this._addBookTypeConfirm();
            });
             $("#add_bookType_quit_btn").on("click", function () {
                _this._closeAllLayer("template_add");
            });
            $("#edit_bookType_confirm_btn").on("click", function () {
                _this._editBookTypeConfirm();
            });
             $("#edit_bookType_quit_btn").on("click", function () {
                _this._closeAllLayer("template_edit");
            });
        },

        _closeAllLayer:function(id){
            $("#"+id).hide();
            layer.closeAll();
        },

        _addBookType:function(){
             $("#template_add").show();
            layer.open({
              id: "addBookTypeLayer",
              type: 1,
              content: $('#add_bookType_form'),
              closeBtn: false
            });
        },

        _addBookTypeConfirm:function(){
            let _this = this;
            let bookTypeName = $("#book_type_name_add").val();
            let borrowDays = $("#borrow_days_add").val();
            if(!bookTypeName){
                layer.msg("类别名称必填");
                return false;
            }
            if(!borrowDays){
                layer.msg("可借天数必填");
                return false;
            }
            let param = {
                "bookTypeName":bookTypeName,
                "borrowDays":borrowDays
            };
            customUtil.post(_this._constant.api.addBookType, param, function (data) {
                if (data.success) {
                    _this._closeAllLayer("template_add");
                    layer.msg(data.msg);
                    _this._renderTable();
                } else {
                    layer.alert(data.messages, {
                        title: '操作失败'
                    })
                }
                $("#book_type_name_add").val("");
                $("#borrow_days_add").val("");
            });
        },

        _addBookTypeQuit:function(){
            layer.closeAll();
        },

        _renderTable: function () {
            let _this = this;
            //执行一个 table 实例
            table.render({
                elem: '#bookTypeTable',
                url: _this._constant.api.queryBookTypes, //数据接口
                method: 'get', //如果无需自定义HTTP类型，可不加该参数
                title: '',
                page: false,//开启分页
                totalRow: false,//开启合计行
                cols: [[ //表头
                    {field: 'bookTypeId', title: '类别ID'},
                    {field: 'bookTypeName', title: '名称'},
                    {field: 'borrowDay', title: '可借天数'},
                    {field: 'createTime', title: '创建时间',templet:function (d) {
                        if(d.createTime){
                            return d.createTime.replace("T"," ").replace("Z"," ");
                        }
                        return d.createTime;
                    }},
                    {field: 'updateTime', title: '更新时间',templet:function (d) {
                        if(d.updateTime){
                            return d.updateTime.replace("T"," ").replace("Z"," ");
                        }
                        return d.updateTime;
                    }},
                    {fixed: 'right', title:'操作', toolbar: '#bookType_table_tool_bar', width:150}
                ]]
            });
        },

         _tableTool: function () {
            let _this = this;
            //监听行工具事件
            table.on('tool(bookType_table_tool_bar_filter)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                let data = obj.data, //获得当前行数据
                layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent === 'edit') {
                    _this._editBookType(data);
                } else if (layEvent === 'delete') {
                   _this._deleteBookType(data);
                }
            });
        },

         _editBookType: function (data) {
            $("#template_edit").show();
            $("#book_type_name_edit").val(data.bookTypeName);
            $("#borrow_days_edit").val(data.borrowDay);
            $("#book_type_id").val(data.bookTypeId);
            let _this = this;
             layer.open({
              id: "editBookTypeLayer",
              type: 1,
              content: $('#edit_bookType_form'),
              closeBtn: false
            });
        },

         _editBookTypeConfirm:function(){
            let _this = this;
            let bookTypeName = $("#book_type_name_edit").val();
            let borrowDays = $("#borrow_days_edit").val();
            let bookTypeId = $("#book_type_id").val();
            if(!bookTypeName){
                layer.msg("类别名称必填");
                return false;
            }
            if(!borrowDays){
                layer.msg("可借天数必填");
                return false;
            }
            let param = {
                "bookTypeName":bookTypeName,
                "borrowDays":borrowDays,
                "bookTypeId":bookTypeId
            };
            customUtil.post(_this._constant.api.editBookType, param, function (data) {
                if (data.success) {
                    _this._closeAllLayer("template_edit");
                    layer.msg(data.msg);
                    _this._renderTable();
                } else {
                    layer.alert(data.messages, {
                        title: '操作失败'
                    })
                }
            });
        },

        _deleteBookType: function (data) {
            let _this = this;
            layer.confirm('确认删除？', {
              btn: ['确定','取消'] //按钮
            }, function(){
                let param = {
                    "bookTypeId" : data.bookTypeId
                };
                customUtil.post(_this._constant.api.deleteBookType, param, function (data) {
                    if (data.success) {
                        layer.msg(data.msg);
                        _this._renderTable();
                    } else {
                        layer.alert(data.messages, {
                            title: '操作失败'
                        })
                    }
                });
            }, function(){
                 layer.closeAll();
            });
        },
    };

    bookType._init();
});